//
//  XDMeHeaderView.m
//  XDEducation
//
//  Created by apple on 2017/4/10.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDMeHeaderView.h"
//#import "UIView+CurrentController.h"

@interface XDMeHeaderView ()
/** 头像 */
@property(nonatomic, strong) UIImageView *iconImageView;

/** 用户名label */
@property(nonatomic, strong) UILabel *nameLabel;


/** button 标题 */
@property(nonatomic,strong) NSArray *btnTitleArray;

@end


#define headerBtnHeight (60 - 5) * ScaleHeight
//图片大小常量
#define iconImageWith 70
//文字大小
#define textFont 14.0

@implementation XDMeHeaderView

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        //self.backgroundColor = [UIColor whiteColor];
        
        
        //背景imageView
       _bgImageView = [[UIImageView alloc]init];
        _bgImageView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height - headerBtnHeight);
        _bgImageView.backgroundColor = ThemeColor;
        [self addSubview:_bgImageView];
        
        //头像imageview
        _iconImageView= [[UIImageView alloc]init ];
        _iconImageView.frame = CGRectMake(0, 0, iconImageWith, iconImageWith);
        _iconImageView.center = CGPointMake(self.centerX, self.centerY - headerBtnHeight/2);
        
        
        //判断登录状态
       // if ([AccountTool shareAccountTool].currentAccount.headImageUrl.length != 0) {
            
            NSString *url = [AccountTool shareAccountTool].currentAccount.headImageUrl;
            NSString *iconUrl = BaseIconUrl(url);
            
            [_iconImageView sd_setImageWithURL:[NSURL URLWithString:iconUrl] placeholderImage:[UIImage imageNamed:@"mine_bnt_user_default"]];
            
//        }else{
//            _iconImageView.image = [UIImage imageNamed:@"mine_bnt_user_default"];
//        }
        
        _iconImageView.layer.masksToBounds = YES;
        _iconImageView.layer.cornerRadius = iconImageWith/2;
        _iconImageView.userInteractionEnabled = YES;
        [self addSubview:_iconImageView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(iconClicked)];
        [_iconImageView addGestureRecognizer:tap];
        
        
        //用户名label
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.frame = CGRectMake(0, self.iconImageView.bottom + 15, ScreenW, 20);
        _nameLabel.font = [UIFont boldSystemFontOfSize:textFont];
        _nameLabel.textColor = [UIColor whiteColor];
        _nameLabel.textAlignment = NSTextAlignmentCenter;
        //判断登录状态
//        if ([AccountTool shareAccountTool].currentAccount.username.length != 0) {
        
            NSString *name = [NSString stringWithFormat:@"%@(%@)",[AccountTool shareAccountTool].currentAccount.username,[AccountTool shareAccountTool].currentAccount.petName];
            _nameLabel.text = name;
//        }else{
//            _nameLabel.text = @"登录";
//        }
        
        [self addSubview:_nameLabel];
        
        
        
        CGFloat buttonX = 0;
        CGFloat buttonW = ScreenW/4;
        CGFloat padding = 1.5;
        for (int i = 0 ; i < self.btnTitleArray.count; i ++) {
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(buttonX + (buttonW + padding) * i, _bgImageView.bottom, buttonW, headerBtnHeight);
            [button setTitle:self.btnTitleArray[i] forState:UIControlStateNormal];
            [button setTitleColor:UIColorFromRGB(0x4c4c4c) forState:UIControlStateNormal];
            [button setBackgroundColor:[UIColor whiteColor]];
            button.titleLabel.font = [UIFont systemFontOfSize:textFont];
            button.tag = i;
            [self addSubview:button];
            [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        }
        
    }
    
    
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, frame.size.height - 5, ScreenW, 5)];
    line.backgroundColor = TableViewBgColor;
    [self addSubview:line];
    
    return self;
}


-(NSArray *)btnTitleArray{

    if (!_btnTitleArray) {
        _btnTitleArray = [NSArray arrayWithObjects:
                          @"三好富豪榜",
                          @"三好银行",
                          @"兑换",
                          @"星星礼包",
                          nil];
    }
    return _btnTitleArray;
}


-(void)buttonClicked:(UIButton *)button{
    
    NSString *btnName = self.btnTitleArray[button.tag];
    
    if (self.functionClickedBlock)
    {
        self.functionClickedBlock (btnName);
    }
}

-(void)iconClicked{

    if (self.iconClickedBlock) {
        self.iconClickedBlock();
    }
}
@end
